package com.xing.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xing.reggie.common.CustomException;
import com.xing.reggie.dto.DishDto;
import com.xing.reggie.entity.Dish;
import com.xing.reggie.entity.DishFlavor;
import com.xing.reggie.mapper.DishMapper;
import com.xing.reggie.service.DishFlavorService;
import com.xing.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;


    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishDto
     */
    @Override
    @Transactional
    public void savaWithFlavor(DishDto dishDto) {

        //保存菜品的基本信息到菜品表中
        this.save(dishDto);
        //获取菜品ID
        Long dishId = dishDto.getId();
        //获取菜品对应的口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        //处理集合，绑定对应菜品Id到口味中
        flavors = flavors.stream().map((item)->{
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
        //保存对应的菜品口味数据到菜品口味表中
        dishFlavorService.saveBatch(flavors);

    }

    /**
     * 根据ID查询菜品信息和口味信息
     * @param id
     * @return
     */
    public DishDto getByIdWithFlavor(Long id){
        //查询菜品基本信息，从dish表查询
        Dish dish = this.getById(id);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        //查询对应的口味信息，从dish_flavor表查
        //条件构造器
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        //查询
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);

        return dishDto;

    }

    /**
     * 更新菜品信息和口味信息
     * @param dishDto
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {

        //更新dish表信息
        this.updateById(dishDto);
        //清除原有口味---dish_flavor表
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);
        //新增口味表信息----dish_flavor
        List<DishFlavor> flavors = dishDto.getFlavors();
        //处理集合，绑定对应菜品Id到口味中
        Long dishId = dishDto.getId();
        flavors = flavors.stream().map((item)->{
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
        //添加口味信息
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 删除菜品信息，同时删除对应的口味信息
     * @param ids
     */
    @Override
    @Transactional
    public void removeWithFlavor(List<Long> ids) {
        //查询菜品信息，决定是否可以删除
        //条件构造器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        queryWrapper.in(Dish::getId,ids);
        queryWrapper.eq(Dish::getStatus,1);
        //查新满足条件数
        int count = count(queryWrapper);
        //如果不满足删除条件，抛出异常！
        if (count >0){
            throw new CustomException("菜品正在售卖中，不能删除！");
        }

        //删除菜品表信息---dish表
        this.removeByIds(ids);
        //删除对应的口味信息---dish_flavor表，由于菜品id不是口味表中主键，所以需要通过条件删除
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        lambdaQueryWrapper.in(DishFlavor::getDishId,ids);
        //通过条件删除
        dishFlavorService.remove(lambdaQueryWrapper);

    }


}
